package com.jlu.yygh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jlu.yygh.common.exception.CustomException;
import com.jlu.yygh.common.helper.JwtHelper;
import com.jlu.yygh.common.result.ResultCodeEnum;
import com.jlu.yygh.enums.AuthStatusEnum;
import com.jlu.yygh.mapper.UserInfoMapper;
import com.jlu.yygh.model.user.Patient;
import com.jlu.yygh.model.user.UserInfo;
import com.jlu.yygh.service.PatientService;
import com.jlu.yygh.service.UserInfoService;
import com.jlu.yygh.vo.user.LoginVo;
import com.jlu.yygh.vo.user.UserAuthVo;
import com.jlu.yygh.vo.user.UserInfoQueryVo;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper,UserInfo> implements UserInfoService {

  @Autowired
  private RedisTemplate<String,String> redisTemplate;

  @Autowired
  private PatientService patientService;


  @Override
  public Map<String, Object>login(LoginVo loginVo) {

    String phone = loginVo.getPhone();
    String code = loginVo.getCode();

    //校验参数，有一方为空则抛出异常
    if (StringUtils.isEmpty(phone) || StringUtils.isEmpty(code)){
      throw new CustomException(ResultCodeEnum.PARAM_ERROR);
    }

    //校验校验验证码
    String mobleCode = redisTemplate.opsForValue().get(phone);
    System.out.println(mobleCode);
    if(!code.equals(mobleCode)) {
      throw new CustomException(ResultCodeEnum.CODE_ERROR);
    }

    //判断是否为第一次注册，是的话则注册，不是的话登录
    QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
    wrapper.eq("phone",phone);
    UserInfo userInfo = baseMapper.selectOne(wrapper);

    System.out.println(userInfo);

    if (userInfo == null){
      userInfo = new UserInfo();
      userInfo.setName("");
      userInfo.setPhone(phone);
      userInfo.setStatus(1);
      this.save(userInfo);
    }

    //校验是否被禁用
    if(userInfo.getStatus() == 0) {
      throw new CustomException(ResultCodeEnum.LOGIN_DISABLED_ERROR);
    }

    //返回用户的信息
    HashMap<String, Object> map = new HashMap<>();
    String name = userInfo.getName();
    if (StringUtils.isEmpty(name)){
      name = userInfo.getNickName();
    }
    if(StringUtils.isEmpty(name)) {
      name = userInfo.getPhone();
    }
    map.put("name", name);

    //生成token字符串
    String token = JwtHelper.createToken(userInfo.getId(), userInfo.getName());
    map.put("token", token);
    System.out.println(map);
    return map;
  }

  @Override
  public void userAuth(Long userId, UserAuthVo userAuthVo) {
    //根据用户id查询用户信息
    UserInfo userInfo = baseMapper.selectById(userId);
    //设置认证信息
    //认证人姓名
    userInfo.setName(userAuthVo.getName());
    //其他认证信息
    userInfo.setCertificatesType(userAuthVo.getCertificatesType());
    userInfo.setCertificatesNo(userAuthVo.getCertificatesNo());
    userInfo.setCertificatesUrl(userAuthVo.getCertificatesUrl());
    userInfo.setAuthStatus(AuthStatusEnum.AUTH_RUN.getStatus());
    //进行信息更新
    baseMapper.updateById(userInfo);
  }

  //用户列表（条件查询带分页）
  @Override
  public Page<UserInfo> selectPage(Page<UserInfo> pageParam, UserInfoQueryVo userInfoQueryVo) {
    //UserInfoQueryVo获取条件值
    String name = userInfoQueryVo.getKeyword(); //用户名称
    Integer status = userInfoQueryVo.getStatus();//用户状态
    Integer authStatus = userInfoQueryVo.getAuthStatus(); //认证状态
    String createTimeBegin = userInfoQueryVo.getCreateTimeBegin(); //开始时间
    String createTimeEnd = userInfoQueryVo.getCreateTimeEnd(); //结束时间
    //对条件值进行非空判断
    QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
    if(!StringUtils.isEmpty(name)) {
      wrapper.like("name",name);
    }
    if(!StringUtils.isEmpty(status)) {
      wrapper.eq("status",status);
    }
    if(!StringUtils.isEmpty(authStatus)) {
      wrapper.eq("auth_status",authStatus);
    }
    if(!StringUtils.isEmpty(createTimeBegin)) {
      wrapper.ge("create_time",createTimeBegin);
    }
    if(!StringUtils.isEmpty(createTimeEnd)) {
      wrapper.le("create_time",createTimeEnd);
    }
    //调用mapper的方法
    Page<UserInfo> pages = baseMapper.selectPage(pageParam, wrapper);
    //编号变成对应值封装
    pages.getRecords().stream().forEach(item -> {
      this.packageUserInfo(item);
    });
    return pages;
  }

  //编号变成对应值封装
  private UserInfo packageUserInfo(UserInfo userInfo) {
    //处理认证状态编码
    userInfo.getParam().put("authStatusString",AuthStatusEnum.getStatusNameByStatus(userInfo.getAuthStatus()));
    //处理用户状态 0  1
    String statusString = userInfo.getStatus().intValue()==0 ?"锁定" : "正常";
    userInfo.getParam().put("statusString",statusString);
    return userInfo;
  }

  @Override
  public void lock(Long userId, Integer status) {
    if(status.intValue() == 0 || status.intValue() == 1) {
      UserInfo userInfo = this.getById(userId);
      userInfo.setStatus(status);
      this.updateById(userInfo);
    }
  }

  //用户详情
  @Override
  public Map<String, Object> show(Long userId) {
    Map<String,Object> map = new HashMap<>();
    //根据userid查询用户信息
    UserInfo userInfo = this.packageUserInfo(baseMapper.selectById(userId));
    map.put("userInfo",userInfo);
    //根据userid查询就诊人信息
    List<Patient> patientList = patientService.findAllUserId(userId);
    map.put("patientList",patientList);
    return map;
  }

  //认证审批  2通过  -1不通过
  @Override
  public void approval(Long userId, Integer authStatus) {
    if(authStatus.intValue()==2 || authStatus.intValue()==-1) {
      UserInfo userInfo = baseMapper.selectById(userId);
      userInfo.setAuthStatus(authStatus);
      baseMapper.updateById(userInfo);
    }
  }
}
